//
//  ConfirmCell.m
//  test
//
//  Created by 朱圳 on 2018/1/5.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "ConfirmCell.h"

@implementation ConfirmCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}
-(void)createUI{
    
    UILabel *timeLab = [[UILabel alloc]init];
    timeLab.text = @"2017-11-13\n9:00-9:45";
    timeLab.numberOfLines = 0;
    timeLab.textColor = [UIColor darkGrayColor];
    timeLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:timeLab];
    [timeLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth/3, 60));
    }];
    self.timeLab = timeLab;
    
    UILabel *nameLab = [[UILabel alloc]init];
    nameLab.text = @"张三";
    nameLab.textColor = [UIColor darkGrayColor];
    nameLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(timeLab.mas_right);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth/3, 60));
    }];
    self.nameLab = nameLab;
    
    UILabel *operationLab = [[UILabel alloc]init];
    operationLab.textColor = [UIColor darkGrayColor];
    operationLab.textAlignment = NSTextAlignmentCenter;
    operationLab.numberOfLines = 0;
    [self.contentView addSubview:operationLab];
    [operationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(nameLab.mas_right);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth/3, 60));
    }];
    self.operationLab = operationLab;
    
    UIButton *acceptBtn = [[UIButton alloc]init];
    acceptBtn.backgroundColor = [UIColor colorWithRed:72/225.0 green:211/255.0 blue:86/255.0 alpha:1.0];
    acceptBtn.layer.cornerRadius = 5;
    [acceptBtn setTitle:@"已上" forState:UIControlStateNormal];
    [acceptBtn addTarget:self action:@selector(accept:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:acceptBtn];
    [acceptBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        [make.centerX.mas_equalTo(operationLab) setOffset:-20];
        make.size.mas_equalTo(CGSizeMake(40, 30));
    }];
    self.acceptBtn = acceptBtn;
    
    UIButton *refuseBtn = [[UIButton alloc]init];
    refuseBtn.backgroundColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
    refuseBtn.layer.cornerRadius = 5;
    [refuseBtn setTitle:@"未上" forState:UIControlStateNormal];
    [refuseBtn addTarget:self action:@selector(refuse:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:refuseBtn];
    [refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        [make.left.mas_equalTo(acceptBtn.mas_right) setOffset:5];
        make.size.mas_equalTo(CGSizeMake(40, 30));
    }];
    self.refuseBtn = refuseBtn;
    
    UIButton *checkBtn = [[UIButton alloc]init];
    checkBtn.backgroundColor = HWColor(255,161,136);
    checkBtn.layer.cornerRadius = 5;
    [checkBtn setTitle:@"点击查看" forState:UIControlStateNormal];
    [self.contentView addSubview:checkBtn];
    [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(nameLab.mas_right) setOffset:10];
        [make.right.mas_equalTo(self.contentView.mas_right) setOffset:-10];
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth/3 - 20, 35));
    }];
    checkBtn.userInteractionEnabled = NO;
    self.checkBtn = checkBtn;
    /**********************分割线******************************/
    //底部
    UIView *xview1 = [[UIView alloc]init];
    xview1.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:xview1];
    [xview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(0.8);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
    }];
    //竖线1
    UIView *xview2 = [[UIView alloc]init];
    xview2.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:xview2];
    [xview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameLab.mas_left);
        make.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.width.mas_equalTo(0.8);
    }];
    
    //竖线2
    UIView *xview3 = [[UIView alloc]init];
    xview3.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:xview3];
    [xview3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(operationLab.mas_left);
        make.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.width.mas_equalTo(0.8);
    }];
}

-(void)accept:(UIButton *)sender{
    
    !self.ClickButtonHandle?:self.ClickButtonHandle(@"1");
    NSLog(@"已上");
    
}
-(void)refuse:(UIButton *)sender{
    
    !self.ClickButtonHandle?:self.ClickButtonHandle(@"2");
    NSLog(@"未上");
    
}
-(void)setMol:(OrderModel *)mol{
    _mol = mol;
    self.checkBtn.hidden = YES;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //设定时间的格式
    NSArray *p=[[Utility getDateStringWithTimeStr:_mol.startTime] componentsSeparatedByString:@" "];
    NSArray *p2=[[Utility getDateStringWithTimeStr:_mol.endTime] componentsSeparatedByString:@" "];
    //NSLog(@"分割后的字符串：%@",p[1]);
    self.timeLab.text = [NSString stringWithFormat:@"%@\n%@-%@",p[0],p[1],p2[1]];
    
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    //  NSLog(@">>>>>>>>>%f",interval);
    if ([_mol.endTime doubleValue]<interval) {
        //  NSLog(@"上课时间完成");
        if ([mol.isornot isEqualToString:@"1"]) {
            
            self.operationLab.hidden = NO;
            self.operationLab.text = @"已上";
            self.operationLab.textColor = [UIColor colorWithRed:72/225.0 green:211/255.0 blue:86/255.0 alpha:1.0];
            self.acceptBtn.hidden = YES;
            self.refuseBtn.hidden = YES;
        }else if ([mol.isornot isEqualToString:@"2"]){
            
            self.operationLab.hidden = NO;
            self.operationLab.text = @"未上";
            self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
            self.acceptBtn.hidden = YES;
            self.refuseBtn.hidden = YES;
        }else{
            self.acceptBtn.hidden = NO;
            self.refuseBtn.hidden = NO;
            self.operationLab.hidden = YES;
        }
    }else{
        self.operationLab.hidden = NO;
        self.operationLab.text = @"待上课";
        self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
        self.acceptBtn.hidden = YES;
        self.refuseBtn.hidden = YES;
    }
}

-(void)setSmol:(OrderModel *)smol{
    
    _smol = smol;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //设定时间的格式
    NSArray *p=[[Utility getDateStringWithTimeStr:_smol.startTime] componentsSeparatedByString:@" "];
    NSArray *p2=[[Utility getDateStringWithTimeStr:_smol.endTime] componentsSeparatedByString:@" "];
    //NSLog(@"分割后的字符串：%@",p[1]);
    self.timeLab.text = [NSString stringWithFormat:@"%@\n%@-%@",p[0],p[1],p2[1]];
    
    //逻辑判断。1.判断当前时间与上课结束的时间 2.先判断老师出来的状态 0未处理 1确定上课 2未上课。3.学生进行处理 0默认未处理 1符合上课 2不符合
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    //  NSLog(@">>>>>>>>>%f",interval);
    if ([_smol.endTime doubleValue]<interval) {
        
        if ([_smol.isornot isEqualToString:@"1"]) {
            
            if ([_smol.sdisnot isEqualToString:@"1"]) {
                
                self.operationLab.hidden = NO;
                self.operationLab.text = @"已上";
                self.operationLab.textColor = [UIColor colorWithRed:72/225.0 green:211/255.0 blue:86/255.0 alpha:1.0];
                self.acceptBtn.hidden = YES;
                self.refuseBtn.hidden = YES;
                self.checkBtn.hidden = YES;
            }else if ([_smol.sdisnot isEqualToString:@"2"]){
                
                self.operationLab.hidden = NO;
                self.operationLab.text = @"未上";
                self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
                self.acceptBtn.hidden = YES;
                self.refuseBtn.hidden = YES;
                self.checkBtn.hidden = YES;
            }else{
                self.checkBtn.hidden = NO;
//                self.acceptBtn.hidden = NO;
//                self.refuseBtn.hidden = NO;
                self.acceptBtn.hidden = YES;
                self.refuseBtn.hidden = YES;
                self.operationLab.hidden = YES;
            }
        }else if ([_smol.isornot isEqualToString:@"0"]){
            self.acceptBtn.hidden = YES;
            self.refuseBtn.hidden = YES;
            self.checkBtn.hidden = YES;
            self.operationLab.text = @"老师待处理";
            self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
        }else{
            self.acceptBtn.hidden = YES;
            self.refuseBtn.hidden = YES;
            self.checkBtn.hidden = YES;
            self.operationLab.text = @"老师确定未上课";
            self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
            self.operationLab.font = [UIFont systemFontOfSize:14];
        }
    }else{
        self.operationLab.hidden = NO;
        self.operationLab.text = @"待上课";
        self.operationLab.textColor = [UIColor colorWithRed:219/225.0 green:83/255.0 blue:80/255.0 alpha:1.0];
        self.acceptBtn.hidden = YES;
        self.refuseBtn.hidden = YES;
        self.checkBtn.hidden = YES;
    }
}
@end

